package com.haoxi.gateway.cmpp30msg;

/**
 * Title:CMPP3.0,2004.09
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * ʵ��CMPP3.0 ��Ϣͷ
 */
public class CMPPMsgHead {
    private byte MsgHead[];
    private int Total_Length;
    private int Command_ID;
    private int Sequence_ID;

    /**
     * ��InBytes�ֽ���ݹ���һ����Ϣͷ
     *
     * @param InBytes ��Ϣͷ
     */
    public CMPPMsgHead ( byte InBytes[] ) throws CMPP30Exception {
        if ( InBytes.length != CMPPCommon.MSG_HEAD_LENGTH )//�����Ϣͷ�ĳ�������
        {
            throw new CMPP30Exception ( "message's head length have error!" );
        }
        int i = 0;
        MsgHead = new byte[InBytes.length];
        for ( i = 0; i < InBytes.length; i++ ) {
            MsgHead[ i ] = InBytes[ i ];
        }
        byte[] tempbytes = new byte[4];
        CMPPCommon.BytesCopy ( MsgHead, tempbytes, 0, 3, 0 );
        Total_Length = CMPPCommon.Bytes4ToInt ( tempbytes );
        CMPPCommon.BytesCopy ( MsgHead, tempbytes, 4, 7, 0 );
        Command_ID = CMPPCommon.Bytes4ToInt ( tempbytes );
        CMPPCommon.BytesCopy ( MsgHead, tempbytes, 8, 11, 0 );
        Sequence_ID = CMPPCommon.Bytes4ToInt ( tempbytes );
    }

    /**
     * ����һ����Ϣͷ
     */
    public CMPPMsgHead () {
        MsgHead = new byte[CMPPCommon.MSG_HEAD_LENGTH];
    }

    /**
     * ����һ����Ϣͷ
     *
     * @param TotalLength �ܳ���
     * @param CommandId   ������
     * @param SequenceID  ���к�
     */
    public CMPPMsgHead ( int TotalLength, int CommandId, int SequenceID ) {
        MsgHead = new byte[CMPPCommon.MSG_HEAD_LENGTH];
        Total_Length = TotalLength;
        Command_ID = CommandId;
        Sequence_ID = SequenceID;
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( TotalLength ), MsgHead, 0, 3, 0 );
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( CommandId ), MsgHead, 0, 3, 4 );
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( SequenceID ), MsgHead, 0, 3, 8 );
    }

    /**
     * �����ܳ���
     *
     * @param CommandLength �ܳ���
     * @return
     */
    public void setCommandLength ( int CommandLength ) {
        Total_Length = CommandLength;
        //MainSystem.logPrint("setCommandLength Total_Length:"+Total_Length);
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( CommandLength ), MsgHead, 0, 3, 0 );
    }

    /**
     * ����������
     *
     * @param CommandID ������
     * @return
     */
    public void setCommandID ( int CommandID ) {
        Command_ID = CommandID;
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( CommandID ), MsgHead, 0, 3, 4 );
    }

    /**
     * ����SequenceId
     *
     * @param SequenceId
     * @return
     */
    public void setSequenceID ( int SequenceId ) {
        Sequence_ID = SequenceId;
        CMPPCommon.BytesCopy ( CMPPCommon.IntToBytes4 ( SequenceId ), MsgHead, 0, 3, 8 );
    }

    /**
     * ȡ����Ϣͷ
     *
     * @param
     * @return ��Ϣͷ
     */
    public byte[] getMsgHead () {
        return MsgHead;
    }

    /**
     * ����Ϣͷ����ƶ����
     *
     * @param out �����
     * @throws IOException ������IOΥ��ʱ
     */
    public void write ( OutputStream out ) throws IOException {
        out.write ( MsgHead );
        out.flush ();
    }

    /**
     * ���ƶ����ȡ����Ϣͷ
     *
     * @param in ������
     * @throws IOException ������IOΥ��ʱ
     */
    public void read ( InputStream in ) throws IOException {
        int readLength;
        byte tempbytes[] = new byte[4];
        readLength = in.read ( tempbytes );
        if ( readLength == -1 ) {
            throw new IOException ( "input stream end!" );
        }
        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 0, 3, 0 );
        Total_Length = CMPPCommon.Bytes4ToInt ( tempbytes );
        readLength = in.read ( tempbytes );
        if ( readLength == -1 ) {
            throw new IOException ( "input stream end!" );
        }
        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 0, 3, 4 );
        Command_ID = CMPPCommon.Bytes4ToInt ( tempbytes );
        readLength = in.read ( tempbytes );
        if ( readLength == -1 ) {
            throw new IOException ( "input stream end!" );
        }
        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 0, 3, 8 );
        Sequence_ID = CMPPCommon.Bytes4ToInt ( tempbytes );
    }

    /**
     * ���ƶ����ȡ����Ϣͷ
     *
     * @param in ������
     * @throws IOException ������IOΥ��ʱ
     */
    public void reada ( InputStream in ) throws IOException {
        int readLength;
        byte tempbytes[] = new byte[12];
        readLength = in.read ( tempbytes );
        if ( readLength == -1 ) {
            throw new IOException ( "input stream end!" );
        }

        byte[] myb = new byte[4];
        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 0, 3, 0 );
        CMPPCommon.BytesCopy ( tempbytes, myb, 0, 3, 0 );
        Total_Length = CMPPCommon.Bytes4ToInt ( myb );

        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 4, 7, 4 );
        CMPPCommon.BytesCopy ( tempbytes, myb, 4, 7, 0 );
        Command_ID = CMPPCommon.Bytes4ToInt ( myb );

        CMPPCommon.BytesCopy ( tempbytes, MsgHead, 8, 11, 8 );
        CMPPCommon.BytesCopy ( tempbytes, myb, 8, 11, 0 );
        Sequence_ID = CMPPCommon.Bytes4ToInt ( myb );
    }

    /**
     * ȡ�������ܳ���
     *
     * @param
     * @return �����ܳ���
     */
    public int getCommandLength () {
        return Total_Length;
    }

    /**
     * ȡ��������
     *
     * @param
     * @return ������
     */
    public int getCommandID () {
        return Command_ID;
    }

    /**
     * ȡ��Sequence
     *
     * @param
     * @return sequence
     */
    public int getSequenceID () {
        return Sequence_ID;
    }
}